package com.mall.hadoop.job.sellstatic;

import java.io.IOException;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * 合并数据输出
 * @author tgy
 *
 */
public class SellStaticReduce extends Reducer<Text, IntWritable, Text, IntWritable>{

	public static Logger logger = LoggerFactory.getLogger(SellStaticReduce.class);
	
	@Override
	protected void reduce(Text key, Iterable<IntWritable> iter,Context context)throws IOException, InterruptedException {
		try{
			int province = 0;
			int city = 0;
			int area = 0;
			String k = key.toString();
			for(IntWritable wr : iter){
				//同一省份园所+1
				province += wr.get();
				//同一城市园所+1
				city += wr.get();
				//同一省份园所+1
				area += wr.get();
			}
			if(k.contains("p")&&!k.contains("c")){
				context.write(key, new IntWritable(province));
			}
			if(k.contains("c")&&k.contains("p")&&!k.contains("a")){
				context.write(key, new IntWritable(city));
			}
			if(k.contains("a")){
				context.write(key, new IntWritable(area));
			}
		}catch(Exception e){
			logger.info("合并园所信息出现异常："+e.toString());
		}
	}
}



